package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AllTreePaths
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-14 15:56
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class AllTreePaths {
    /**
     * 257. 二叉树的所有路径
     * 给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
     * <p>
     * 叶子节点 是指没有子节点的节点。
     */


    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();

        dfs(root, "", res);
        return res;
    }

    public static void dfs(TreeNode root, String path, List<String> res) {
        if (root == null) return;
        //说明是叶子节点
        if (root.left == null && root.right == null) {
            res.add(path + root.val);
            return;
        }

        dfs(root.left, path + root.val + "->", res);
        dfs(root.right, path + root.val + "->", res);
    }


    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(2);

        TreeNode treeNodeLeft = new TreeNode(1);
        treeNode.left = treeNodeLeft;

        TreeNode treeNodeRight = new TreeNode(3);
        treeNodeLeft.right = treeNodeRight;

        AllTreePaths allTreePaths = new AllTreePaths();
        System.out.println(allTreePaths.binaryTreePaths(treeNode));

    }
}